package com.ms.game.invitefriend.async;

import com.ms.game.common.container.GameLimitInfo;
import com.ms.game.invitefriend.db.DistributorInviteCodeDAO;
import com.ms.game.invitefriend.db.DistributorsModel;
import com.ms.game.invitefriend.db.SubDistribuorModifyLogModel;
import com.ms.game.invitefriend.db.SubDistributorsModel;
import com.ms.game.invitefriend.message.SDistributorSettingNotfiy;
import com.ms.game.invitefriend.message.SModifySubDistributorCommSettingResponse;
import com.ms.game.invitefriend.message.SubDistributorInfo;
import com.ms.player.Player;
import com.ms.player.PlayerIdentityType;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class ModifySubDistributorCommSettingAsync extends TaskAsyncCommand{

	public Player player;
	
	public String subDistributorName;
	
	public int modifyInivteCodeNum;
	
	public int modifyDesignatedLevelMoney;
	
	public int modifyLevelUpMoney;
	
	public GUID sessionId;
	
	public int messageType;
	
	public BasicServiceModule module;
		
	@Override
	public void asyncExecute() throws Exception {
		SModifySubDistributorCommSettingResponse response = new SModifySubDistributorCommSettingResponse();

		Player subDistriborPlayerData = PlayerDAO.loadPlayerByName(subDistributorName);
		if(subDistriborPlayerData == null) {
			response.setErrorCode(ErrorCode.TARGET_NOT_SUB_DISTRIBUTOR);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		//對方角色必須50等
		int limitCode = GameLimitInfo.checkLimitByClassify(GameLimitInfo.PUSH_INVIT_CODE, subDistriborPlayerData.getVip(), subDistriborPlayerData.getLevel(), subDistriborPlayerData.isVerifiedMember(), subDistriborPlayerData.isTrialAccount(), subDistriborPlayerData.getUplineCode());
		if (limitCode != GameLimitInfo.TRUE) {
			response.setErrorCode(ErrorCode.TARGET_PLAYER_NEED_VERIFY_AND_LEVEL_NEED_FIFTY_WHEN_MODIFY);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		if(subDistriborPlayerData.getPlayerIdentityType() != PlayerIdentityType.SUB_DISTRIBUTOR){
			response.setErrorCode(ErrorCode.TARGET_NOT_SUB_DISTRIBUTOR);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		SubDistributorsModel subDistributorsModel = DistributorInviteCodeDAO.getSubDistributorsModelByGuid(subDistriborPlayerData.getGuid().toString());
		if(subDistributorsModel == null) {
			response.setErrorCode(ErrorCode.TARGET_NOT_SUB_DISTRIBUTOR);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		DistributorsModel distributorsModel = DistributorInviteCodeDAO.getDistributorModelByGUID(player.getGuid().toString());
		if(distributorsModel == null || !distributorsModel.isEffective()){
			response.setErrorCode(ErrorCode.PLAYER_NOT_DISTRIBUTOR);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		//------------------------ 判斷結束 ------------------------ //
		//寫log
		int originInivteCodeNum = subDistributorsModel.getInviteCodeNum();
		int originLevelUpComm = subDistributorsModel.getLevelUpMoneyComm();
		int originDesignatedLeveMoneyComm = subDistributorsModel.getDesignatedLeveMoneyComm();

		//先算數量
		int count = modifyInivteCodeNum - originInivteCodeNum;
		boolean modifyInviteCodeSuccess = false;
		if (count == 0) {
			if (subDistributorsModel.getInviteCodeNum() == 0) {
				// 原本就沒有設定過
				response.setErrorCode(ErrorCode.MODIFY_SUB_DISTRIBUTOR_INVITE_CODE_NUM_ERROR);
				MessageAction.sendMessage(sessionId, messageType, response);
				return;
			}
		} else if(count > 0){
			//增加
			//檢查經銷商購不夠
			int leftCount = DistributorInviteCodeDAO.getLeftDistributorInviteCodeCount(distributorsModel.getGuid(), PlayerIdentityType.DISTRIBUTOR);
			if(leftCount < count){
				response.setErrorCode(ErrorCode.INVITE_CODE_NOT_ENOUGH);
				MessageAction.sendMessage(sessionId, messageType, response);
				return;
			}
						
			if(!DistributorInviteCodeDAO.modifySubDistributorInviteCodeNum(subDistributorsModel, subDistributorsModel.getInviteCodeNum(), count)){
				response.setErrorCode(ErrorCode.INVITE_CODE_NOT_ENOUGH);
				MessageAction.sendMessage(sessionId, messageType, response);
				return;
			}
			modifyInviteCodeSuccess = true;
		}else if (count < 0){
			//減少
			//要先檢查子經銷商夠不夠扣
			int leftCount = DistributorInviteCodeDAO.getLeftDistributorInviteCodeCount(subDistributorsModel.getSubDistributorGuid(), PlayerIdentityType.SUB_DISTRIBUTOR);
			if(leftCount <  Math.abs(count)) {
				response.setErrorCode(ErrorCode.INVITE_CODE_NOT_ENOUGH);
				MessageAction.sendMessage(sessionId, messageType, response);
				return;
			}
			
			if (!DistributorInviteCodeDAO.modifySubDistributorInviteCodeNum(subDistributorsModel, subDistributorsModel.getInviteCodeNum(), count)) {
				response.setErrorCode(ErrorCode.TARGET_INVITE_CODE_NOT_ENOUGH_TO_SUBTRACT);
				MessageAction.sendMessage(sessionId, messageType, response);
				return;
			}
			modifyInviteCodeSuccess = true;
		}
		
		if(modifyInviteCodeSuccess){
			//如果邀請碼數量有任何調整，要跟子經銷商說
			GUID subSession = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(subDistriborPlayerData.getGuid());
			//在線上才傳給他
			if (subSession != null) {
				SDistributorSettingNotfiy notify = new SDistributorSettingNotfiy();
				notify.setErrorCode(ErrorCode.SUCCESS);
				notify.setDistributorName(distributorsModel.getPlayerName());
				MessageAction.sendMessage(subSession, MessageTypes.S_INVITE_CODE_DISTRIBUTOR_SETTING_NOTIFY, notify);
			}
		}

		if( modifyLevelUpMoney > distributorsModel.getLevelUpMoney()) {
			//每等級不能抽的比經銷商的上限多
			response.setErrorCode(ErrorCode.MODIFY_LEVEL_UP_MONEY_COMM_ERROR);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		if(modifyDesignatedLevelMoney > distributorsModel.getDesignatedLeveMoney()){
			//不能超過設定的指定等級抽傭
			response.setErrorCode(ErrorCode.MODIFY_DESIGNATED_LEVEL_MONEY_ERROR);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		boolean isModify = subDistributorsModel.isModify();
		boolean needCompareModify = isModify && 
					!(subDistributorsModel.getModifyLevelUpMoneyComm() == modifyLevelUpMoney && 
						subDistributorsModel.getModifyDesignatedLeveMoneyComm() == modifyDesignatedLevelMoney);
		boolean needCompareNow = !isModify && 
					!(subDistributorsModel.getLevelUpMoneyComm() == modifyLevelUpMoney && 
							subDistributorsModel.getDesignatedLeveMoneyComm() == modifyDesignatedLevelMoney);
		
		if(needCompareModify || needCompareNow) {
			DistributorInviteCodeDAO.modifySubDistributorCommPreSetting(subDistributorsModel, modifyLevelUpMoney, modifyDesignatedLevelMoney);
		}
		//------------ 回覆  -------------
		SubDistributorInfo info = new SubDistributorInfo();
		info.setLevel(subDistriborPlayerData.getLevel());
		info.setName(subDistributorsModel.getSubDistributorName());
		info.setModify(subDistributorsModel.isModify());
		info.setDesignatedLeveMoneyComm(subDistributorsModel.getDesignatedLeveMoneyComm());
		info.setLevelUpMoneyComm(subDistributorsModel.getLevelUpMoneyComm());
		
		info.setModifyDesignatedLeveMoneyComm(subDistributorsModel.getModifyDesignatedLeveMoneyComm());
		info.setModifyLevelUpMoneyComm(subDistributorsModel.getModifyLevelUpMoneyComm());
		
		info.setInvteCodeCount(subDistributorsModel.getInviteCodeNum());
		int subUsedNum = DistributorInviteCodeDAO.getUsedDistributorInviteCodeNum(subDistriborPlayerData.getGuid().toString(),PlayerIdentityType.SUB_DISTRIBUTOR);
		info.setUsedCount(subUsedNum);
		
		int disLeftNum =  DistributorInviteCodeDAO.getLeftDistributorInviteCodeCount(distributorsModel.getGuid(), PlayerIdentityType.DISTRIBUTOR);
		response.setDistributorInviteCodeLeftNum(disLeftNum);
		response.setDistributorInviteCodeNum(distributorsModel.getInviteCodeCount());
		
		response.setSubDistributorInfo(info);
		response.setErrorCode(ErrorCode.SUCCESS);
		MessageAction.sendMessage(sessionId, messageType, response);
		
		//------------- log ----------------//
		SubDistribuorModifyLogModel log = new SubDistribuorModifyLogModel();
		log.setFirestSetting(!subDistributorsModel.isAfterFirstSetting());
		log.setModifyTime(subDistributorsModel.getModifyTime());
		log.setDistributorGuid(subDistributorsModel.getDistributorGuid());
		log.setDistributorName(distributorsModel.getPlayerName());
		log.setSubDistributorGuid(subDistributorsModel.getSubDistributorGuid());
		log.setSubDistributorName(subDistributorsModel.getSubDistributorName());
		
		log.setInviteCodeNum(originInivteCodeNum);
		log.setLevelUpMoneyComm(originLevelUpComm);
		log.setDesignatedLeveMoneyComm(originDesignatedLeveMoneyComm);

		log.setModifyInivteCodeNum(subDistributorsModel.getInviteCodeNum());
		log.setModifyLevelUpMoneyComm(modifyLevelUpMoney);
		log.setModifyDesignatedLeveMoneyComm(modifyDesignatedLevelMoney);
		module.saveDbLog(log);
	}

	@Override
	public void execute() throws Exception {
		
	}

}
